package cn.juque.systemservice.service.provider;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.juque.common.annotation.HandlerAppException;
import cn.juque.common.base.BaseResponseDTO;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.fastjson.abstracts.AbstractTypeReference;
import cn.juque.common.thread.BaseOperatorThreadLocal;
import cn.juque.common.utils.FastJsonUtil;
import cn.juque.common.utils.ValidatorUtil;
import cn.juque.systemservice.api.dto.area.*;
import cn.juque.systemservice.api.provider.IAreaInfoProvider;
import cn.juque.systemservice.bo.AreaInfoBO;
import cn.juque.systemservice.domain.AreaInfoDO;
import cn.juque.systemservice.entity.AreaInfo;
import cn.juque.systemservice.service.IAreaInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author JUQUE
 * @version 1.0.0
 * <ul>
 *  <li>simple-generator</li>
 *  <li>地区信息</li>
 * </ul>
 * @date 2022-10-27 21:15:33
 **/
@Slf4j
@Service("areaInfoProvider")
@HandlerAppException
public class AreaInfoProviderImpl implements IAreaInfoProvider {

    @Resource
    private BaseOperatorThreadLocal baseOperatorThreadLocal;

    @Resource
    private IAreaInfoService areaInfoService;

    @Resource
    private ValidatorUtil validatorUtil;

    /**
     * 列表检索
     *
     * @param searchDTO 入参
     * @return List
     */
    @Override
    public BaseResponseDTO<List<AreaInfoListDTO>> list(AreaInfoSearchDTO searchDTO) {
        this.validatorUtil.checkNotNull(searchDTO);
        PageInfo pageInfo = searchDTO.getPageInfo();
        AreaInfoDO areaInfo = new AreaInfoDO();
        BeanUtil.copyProperties(searchDTO, areaInfo);
        List<AreaInfo> infoList = this.areaInfoService.listInfoWithoutPage(areaInfo);
        List<AreaInfoListDTO> infoDtoList = BeanUtil.copyToList(infoList, AreaInfoListDTO.class);
        return new BaseResponseDTO<>(MessageEnum.OK, infoDtoList, pageInfo);
    }

    /**
     * 地区编码查询
     *
     * @param searchByCodeDTO AreaSearchByCodeDTO
     * @return List
     */
    @Override
    public BaseResponseDTO<List<AreaInfoListDTO>> listByCode(AreaSearchByCodeDTO searchByCodeDTO) {
        this.validatorUtil.checkNotNull(searchByCodeDTO);
        this.validatorUtil.fastValidate(searchByCodeDTO);
        List<AreaInfoBO> list = this.areaInfoService.listByCode(searchByCodeDTO.getAreaCodeList());
        List<AreaInfoListDTO> dtoList;
        if (Boolean.TRUE.equals(searchByCodeDTO.getContainChild())) {
            dtoList = FastJsonUtil.parseArray(FastJsonUtil.toJsonString(list), AreaInfoListDTO.class);
        } else {
            dtoList = BeanUtil.copyToList(list, AreaInfoListDTO.class, new CopyOptions(null, false, "children"));
        }
        return new BaseResponseDTO<>(MessageEnum.OK, dtoList);
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return AreaInfoDetailDTO
     */
    @Override
    public BaseResponseDTO<AreaInfoDetailDTO> detail(String id) {
        AreaInfoBO areaInfo = this.areaInfoService.detail(id);
        AreaInfoDetailDTO detailDTO = FastJsonUtil.parseObject(FastJsonUtil.toJsonString(areaInfo), new AbstractTypeReference<AreaInfoDetailDTO>() {
        });
        return new BaseResponseDTO<>(MessageEnum.OK, detailDTO);
    }

    /**
     * 新增
     *
     * @param addDTO 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> add(AreaInfoAddDTO addDTO) {
        this.validatorUtil.fastValidate(addDTO);
        this.baseOperatorThreadLocal.set(addDTO.getBaseOperator());
        AreaInfo areaInfo = new AreaInfo();
        BeanUtil.copyProperties(addDTO, areaInfo);
        this.areaInfoService.saveInfo(areaInfo);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }

    /**
     * 更新
     *
     * @param updateDTO 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> update(AreaInfoUpdateDTO updateDTO) {
        this.validatorUtil.fastValidate(updateDTO);
        this.baseOperatorThreadLocal.set(updateDTO.getBaseOperator());
        AreaInfo areaInfo = new AreaInfo();
        BeanUtil.copyProperties(updateDTO, areaInfo);
        this.areaInfoService.updateInfo(areaInfo);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }

    /**
     * 删除
     *
     * @param req 入参
     * @return Boolean
     */
    @Override
    public BaseResponseDTO<Boolean> delete(AreaInfoDeleteDTO req) {
        List<String> infoIdList = req.getIdList();
        this.areaInfoService.deleteInfo(infoIdList);
        return new BaseResponseDTO<>(MessageEnum.OK, Boolean.TRUE);
    }
}
